# -*- coding: utf-8 -*-


def get_configparser(file):
    setting_dict = {}
    with open(file) as file_object:
        lines = file_object.readlines()

    for line in lines:
        line = line.strip()
        if line.startswith(r'##') or not len(line) or '=' not in line:
            continue

        line_list = line.split(r'=')

        # Delete space
        for index, item in enumerate(line_list):
            line_list[index] = item.strip()

        # Delete the first quote(")
        if line_list[1].startswith(r'"'):
            line_list[1] = line_list[1].lstrip(r'"')

        # role: ,seed
        if line_list[1].startswith(r','):
            line_list[1] = line_list[1].lstrip(r',')

        # Delete the last quote(")
        if line_list[-1].endswith(r'"'):
            line_list[-1] = line_list[-1][0:-1]

        key = line_list[0]
        value = '='.join(line_list[1:]).strip(r"'")

        if r'"' in value and r'\\' not in value:
            value = value.replace(r'"', r'\\"')

        if key.startswith('# '):
            key = "{0}_comment".format(key.split()[1])

        setting_dict[key] = value

    return setting_dict

